package cn.itsource.order.service.impl;

import cn.itsource.basic.constant.AlipayConfig;
import cn.itsource.basic.constant.PayConstants;
import cn.itsource.basic.constant.PetHomeConstant;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.order.domain.AdoptOrder;
import cn.itsource.order.domain.AlipayInfo;
import cn.itsource.order.domain.PayBill;
import cn.itsource.order.mapper.AdoptOrderMapper;
import cn.itsource.order.mapper.AlipayInfoMapper;
import cn.itsource.order.mapper.PayBillMapper;
import cn.itsource.order.service.IPayBillService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill>implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Override
    public String goPay(PayBill payBill) {
        //请求
        String result = null;
        try {
            //根据店铺id查询阿里支付信息对象
            AlipayInfo alipayInfo = alipayInfoMapper.findByShopId(payBill.getShop().getId());
            //获得初始化的AlipayClient
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl,
                    alipayInfo.getAppid(), alipayInfo.getMerchant_private_key(), "json",
                    AlipayConfig.charset, alipayInfo.getAlipay_public_key(), AlipayConfig.sign_type);

            //设置请求参数
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            //设置同步请求
            alipayRequest.setReturnUrl(AlipayConfig.return_url);
            //设置异步请求
            alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
            //商户订单号，商户网站订单系统中唯一订单号，必填
            String out_trade_no = payBill.getOrderSn();
            //付款金额，必填
            String total_amount = payBill.getMoney().toString();
            //订单名称，必填
            String subject = payBill.getDigest();
            //商品描述，可空
            String body = payBill.getDigest();

            alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                    + "\"total_amount\":\""+ total_amount +"\","
                    + "\"subject\":\""+ subject +"\","
                    + "\"body\":\""+ body +"\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
            result = alipayClient.pageExecute(alipayRequest).getBody();
            return result;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }
    //支付成功后处理订单信息
    @Override
    public void handleOrder(String out_trade_no, String trade_no) {
        //通过订单号查询支付单对象
        PayBill payBill=payBillMapper.findByOrderSn(out_trade_no);
        //设置支付单号
        payBill.setUnionPaySn(trade_no);
        //设置交易完成时间
        payBill.setUpdateTime(new Date());
        //设置交易状态 已支付
        payBill.setState(PetHomeConstant.FINISH);
        //修改
        payBillMapper.update(payBill);
        //根据业务类型判断修改那个表的数据
        switch (payBill.getBusinessType()){
            case PayConstants.BUSINESSTYPE_ADOPT:
                //根据支付单的的类型主键id查询订单的对应的数据
                AdoptOrder order = adoptOrderMapper.findById(payBill.getBusinessKey());
                //设置支付单号
                order.setPaySn(trade_no);
                //设置支付时间
                order.setLastConfirmTime(new Date());
                //设置订单状态为待收货
                order.setState(PetHomeConstant.UNRECEIVED);
                //修改订单
                adoptOrderMapper.update(order);
                break;
            case PayConstants.BUSINESSTYPE_GOODS:
                break;
            case PayConstants.BUSINESSTYPE_PRODUCT:
                break;
        }
    }
}
